home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / normalize.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  1.7 KB  |  60 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. // Alias|Wavefront Script File
  19. // MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //<doc>
  22. //<name normalize>
  23. //<owner "Alias|Wavefront Unsupported">
  24. //
  25. //<synopsis>
  26. //        int normalize( float $v[] )
  27. //
  28. //<description>
  29. //        Normalizes a float array of 3 values.
  30. //
  31. //<flags>
  32. //        float[]    $v    Array of 3 floats to be normalized
  33. //
  34. //<returns>
  35. //        int : -1 if the float array was zero length.
  36. //               1 if the normalized vector was successfully calculated.
  37. //
  38. //</doc>
  39. //
  40. global proc int normalize( float $v[] )
  41. {
  42.     if ( size($v) != 3 ) 
  43.         warning("normalize(): You must give a float array of 3 values as the argument.");
  44.  
  45.     float $factor = 1.0;
  46.     float $len = $v[0]*$v[0] + $v[1]*$v[1] + $v[2]*$v[2];
  47.  
  48.     if( $len == 0.0 ) return -1;
  49.     else $factor = 1.0/sqrt($len);
  50.  
  51.     if ( $factor != 1.0 )
  52.     {
  53.         $v[0] *= $factor;
  54.         $v[1] *= $factor;
  55.         $v[2] *= $factor;
  56.     }
  57.  
  58.     return 1;
  59. }
  60.